package com.general.app.xtgl.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import com.general.app.xtgl.dao.IAssessUserDao;
import com.general.app.xtgl.dao.entity.AssessUserModel;
import com.general.app.xtgl.dao.entity.RelationModel;
import com.general.app.xtgl.service.IAssessUserService;
import com.general.common.exception.BusinessException;
import com.general.common.service.impl.BaseServiceImpl;
import com.general.utils.base.Constant;
import com.general.utils.base.StringUtil;

/**
 * 用户信息管理 
 * 
 * @author qilm
 *
 */
public class AssessUserServiceImpl extends BaseServiceImpl<AssessUserModel, IAssessUserDao> implements IAssessUserService{

	/**
	 * 保存用户信息
	 */
	public boolean saveAssessUser(AssessUserModel assessUser) throws BusinessException {
		boolean returnFlg = false;
		int count = dao.saveAssessUser(assessUser);
		if(count >0){
			returnFlg = true;
		}
		return returnFlg;
	}
	
	/**
	 * 批量删除用户信息
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public String scAssessUser(String ids) throws Exception {
		
		if (!StringUtil.isEmpty(ids)) {
			String[] pks = ids.split(",");
			List<AssessUserModel> list = new ArrayList<AssessUserModel>();		
			// 删除被评估者的用户关系
			List<HashMap<String, String>> delLst = new ArrayList<HashMap<String, String>>();
			
			for (int i = 0; i < pks.length; i++) {
				AssessUserModel model = new AssessUserModel();
				model.setUser_id(pks[i]);
				list.add(model);
				HashMap<String, String> map = new HashMap<String,String>();
				map.put("assess_user_id", pks[i]);
				delLst.add(map);
			}
			
			// 删除关系
			int count = dao.deleteRelation(delLst);
			
			count = dao.batchDelete(list);
			return count==pks.length ? "删除成功！" : "成功删除用户信息"+count+"条,"+(pks.length-count)+"条由于与其它业务关联不能删除！";
			
		}
		throw new Exception("删除列值为空！");
	}
	

	/**
	 * 匹配用户关系LIST
	 * @param user_id 
	 * @param companyId
	 * @return
	 */
	public List<RelationModel> getRelationList(String userId, String companyId){
		HashMap<String, String> map = new HashMap<String,String>();
		map.put("user_id", userId);
		map.put("company_id", companyId);
		return dao.getRelationList(map);
	}
	
	
	/**
	 * 更新用户关系
	 * @param model
	 * @return
	 */
	public boolean updateRelation(AssessUserModel model){
		
		// 被评估者ID
		String assessUserId = model.getUser_id();

		// 删除被评估者的用户关系
		List<HashMap<String, String>> delLst = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String,String>();
		map.put("assess_user_id", assessUserId);
		delLst.add(map);
		int count = dao.deleteRelation(delLst);
		
		// 追加被评估者的用户关系
		List<HashMap<String, String>> lstRelation = new ArrayList(getRelationLst(model));
		
		if(lstRelation!=null&& lstRelation.size()!=0) {
			int countIn = dao.batchInsertRelation(lstRelation);
		}
		return true;
	}
 
	/**
	 * 
	 * @param model
	 * @return
	 */
	private Set getRelationLst(AssessUserModel model){
		
		// 追加被评估者的用户关系
		Set relationSet = new HashSet();
		relationSet.clear();
		
		// 被评估者ID
		String assessUserId = model.getUser_id().trim();
		
		// 上级
		String superior = model.getSuperior() == null ? "" : model
				.getSuperior();

		// 平级
		String equal = model.getEqual() == null ? "" : model.getEqual();

		// 下级
		String subordinate = model.getSubordinate() == null ? "" : model
				.getSubordinate();

		// 追加上级
		if(!StringUtil.isEmpty(superior)){
			String[] superiors = superior.split(",");
			for(String s:superiors){
				// 追加上级
				HashMap<String, String> superiorMap = new HashMap<String,String>();
				superiorMap.put("assess_user_id", assessUserId);
				superiorMap.put("relation", Constant.SUPERIOR);
				superiorMap.put("user_id", s.trim());
				relationSet.add(superiorMap);
				
//				//追加对应的下级
//				superiorMap = new HashMap<String,String>();
//				superiorMap.put("assess_user_id", s.trim());
//				superiorMap.put("relation", Constant.SUBORDINATE);
//				superiorMap.put("user_id", assessUserId);
//				relationSet.add(superiorMap);
			}			
		}
		// 追加平级
		if(!StringUtil.isEmpty(equal)){
			String[] equals = equal.split(",");
			for(String s:equals){
				//追加对应的平级
				HashMap<String, String> equalsMap = new HashMap<String,String>();
				equalsMap.put("assess_user_id", assessUserId);
				equalsMap.put("relation", Constant.EQUAL);
				equalsMap.put("user_id", s.trim());
				relationSet.add(equalsMap);

//				//追加对应的平级
//				equalsMap = new HashMap<String,String>();
//				equalsMap.put("assess_user_id", s.trim());
//				equalsMap.put("relation", Constant.EQUAL);
//				equalsMap.put("user_id", assessUserId);
//				relationSet.add(equalsMap);
			}			
		}
		// 追加下级
		if(!StringUtil.isEmpty(subordinate)){
			String[] subordinates = subordinate.split(",");
			for(String s:subordinates){
				//追加对应的下级
				HashMap<String, String> subordinateMap = new HashMap<String,String>();
				subordinateMap.put("assess_user_id", assessUserId);
				subordinateMap.put("relation", Constant.SUBORDINATE);
				subordinateMap.put("user_id", s.trim());
				relationSet.add(subordinateMap);

//				//追加对应的上级
//				subordinateMap = new HashMap<String,String>();
//				subordinateMap.put("assess_user_id", s.trim());
//				subordinateMap.put("relation", Constant.SUPERIOR);
//				subordinateMap.put("user_id", assessUserId);
//				relationSet.add(subordinateMap);
			}
		}
		return relationSet;
	}
	
	/**
	 * 取出用户信息列表
	 * @param usrModel
	 * @return
	 */
	public List<AssessUserModel> getUserList(AssessUserModel usrModel){
		
		return dao.getUserList(usrModel);
	}

	/**
	 * 查询单条数据
	 * @param t
	 * @return
	 */
	public AssessUserModel getModel(AssessUserModel t){
		return dao.getModelByNameEmailPhone(t);
	}
	
	/**
	 * 取出用户关系信息列表
	 * @param id 被评估者ID(_分割)
	 * @return
	 */
	public List<AssessUserModel> getAssessList(String ids){
		if(ids==null || "".equalsIgnoreCase(ids)) return null;
		List<AssessUserModel> lst = new ArrayList<AssessUserModel>();
		String[] pks = ids.split("_");
		for (String pk:pks){
			AssessUserModel model = new AssessUserModel();
			model.setAssess_user_id(pk.trim());
			lst.add(model);
		}
		return dao.getAssessList(lst);
	}
	
	
	/**
	 * 批量更新用户AssessFlg=>0
	 * @param string
	 */
	public void batchUpdateAssessFlg(String ids){
		if(ids==null || "".equalsIgnoreCase(ids)) return;
		List<AssessUserModel> lst = new ArrayList<AssessUserModel>();
		String[] pks = ids.split(",");
		for (String pk:pks){
			AssessUserModel model = new AssessUserModel();
			model.setUser_id(pk.trim());
			lst.add(model);
		}
		dao.batchUpdateAssessFlg(lst);		
	}
	

	/**
	 * 批量插入用户
	 * @param insertUserLst
	 */
	@Override
	public int batchInsert(List<AssessUserModel> insertUserLst){
		int count =  dao.batchInsert(insertUserLst);
		return count;
	}
	

	/**
	 * 批量插入用户关系
	 * @param relationModelLst
	 */
	public int batchInsertRelation(List<AssessUserModel> relationModelLst){
		
		int count = 0;
		Set assessSet =new HashSet();
		Set relationSetAll =new HashSet();
		for(AssessUserModel user : relationModelLst){
			HashMap<String, String> map = new HashMap<String,String>();
			map.put("assess_user_id", user.getUser_id());
			assessSet.add(map);
			
			List<HashMap<String, String>> lstRelation = new ArrayList<HashMap<String, String>>();
			Set relationSet = getRelationLst(user);
			relationSetAll.addAll(relationSet);
		}

		// 追加被评估者的用户关系
		List<HashMap<String, String>> delLst = new ArrayList<HashMap<String, String>>(assessSet);
		
		if(delLst != null && delLst.size() != 0) {
			count = dao.deleteRelation(delLst);
		}
		
		// 追加被评估者的用户关系
		List<HashMap<String, String>> lstRelation = new ArrayList(relationSetAll);
		
		if(lstRelation!=null&& lstRelation.size()!=0) {
			count = dao.batchInsertRelation(lstRelation);
		}
		
		return count;
	}
	
}
